from DataPrepare.dailyFactorsWithInfluxdb.factorBase import factorBase
import cx_Oracle as oracle
import pandas as pd
from Config.myConfig import *
from Config.myConstant import *
from DataAccess.TradedayDataProcess import TradedayDataProcess

########################################################################
class AShareYield(factorBase):
    """记录A股收益数据等"""
    #----------------------------------------------------------------------
    def __init__(self):
        #super(buySellVolumeRatio,self).__init__()
        super().__init__()
        self.factor='dailyAShareYield'
        pass
    #----------------------------------------------------------------------
    def getDataFromOracleByCode(self,code,startDate,endDate):
        data=self.__getDailyDataByDateFromOracleServer(code,startDate,endDate);
        return data
        pass
    #----------------------------------------------------------------------
    #输入code=600000.SH，startdate=yyyyMMdd，endDate=yyyyMMdd
    def __getDailyDataByDateFromOracleServer(self,code,startDate=EMPTY_STRING,endDate=EMPTY_STRING):
        #获取行情数据
        #附注status -1:交易-2:待核查0:停牌XD:除息XR:除权DR:除权除息N:上市首日
        database='wind_filesync.AShareYield'
        connection = oracle.connect(self.oracleConnectStr)
        cursor = connection.cursor()
        oracleStr="select S_INFO_WINDCODE as code,TRADE_DT as \"date\",PCT_CHANGE_D,PCT_CHANGE_W,PCT_CHANGE_M,TURNOVER_D,TURNOVER_D_FLOAT,TURNOVER_W,TURNOVER_W_FLOAT,TURNOVER_M,TURNOVER_M_FLOAT,BETA_DAY_1Y,BETA_DAY_2Y,ALPHA_DAY_1Y,ALPHA_DAY_2Y FROM wind_filesync.AShareYield "
        if startDate==EMPTY_STRING:
            oracleStr=oracleStr+"where S_INFO_WINDCODE='{0}' order by TRADE_DT".format(code)
        elif endDate==EMPTY_STRING:
            oracleStr=oracleStr+"where S_INFO_WINDCODE='{0}' and TRADE_DT>={1} order by TRADE_DT".format(code,startDate)
        else:
            oracleStr=oracleStr+"where S_INFO_WINDCODE='{0}' and TRADE_DT>={1} and TRADE_DT<={2} order by TRADE_DT".format(code,startDate,endDate)
        cursor.execute(oracleStr)


        mydata = cursor.fetchall()
        mydata = pd.DataFrame(mydata,columns=['code','date','PCT_CHANGE_D','PCT_CHANGE_W','PCT_CHANGE_M','TURNOVER_D','TURNOVER_D_FLOAT','TURNOVER_W','TURNOVER_W_FLOAT','TURNOVER_M','TURNOVER_M_FLOAT','BETA_DAY_1Y','BETA_DAY_2Y','ALPHA_DAY_1Y','ALPHA_DAY_2Y'])
        mydata[['PCT_CHANGE_D','PCT_CHANGE_W','PCT_CHANGE_M','TURNOVER_D','TURNOVER_D_FLOAT','TURNOVER_W','TURNOVER_W_FLOAT','TURNOVER_M','TURNOVER_M_FLOAT','BETA_DAY_1Y','BETA_DAY_2Y','ALPHA_DAY_1Y','ALPHA_DAY_2Y']] = mydata[['PCT_CHANGE_D','PCT_CHANGE_W','PCT_CHANGE_M','TURNOVER_D','TURNOVER_D_FLOAT','TURNOVER_W','TURNOVER_W_FLOAT','TURNOVER_M','TURNOVER_M_FLOAT','BETA_DAY_1Y','BETA_DAY_2Y','ALPHA_DAY_1Y','ALPHA_DAY_2Y']].astype('float')
        mydata['mytime']=pd.to_datetime(mydata['date'],format='%Y%m%d')
        mydata.set_index('mytime',inplace=True,drop=True)
        return mydata  
########################################################################
